﻿#region License
// Copyright © 2004 - 2010 All Right Reserved, Derek Goslin
// 
// 1. Definitions
// The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
// A "contribution" is the original software, or any additions or changes to the software.
// A "contributor" is any person that distributes its contribution under this license.
// "Licensed patents" are a contributor's patent claims that read directly on its contribution.
// 
// 2. Grant of Rights
// (A) Copyright Grant- Subject to the terms of this license, including the license
//     conditions and limitations in section 3, each contributor grants you a non-exclusive,
//     worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative 
//     works of its contribution, and distribute its contribution or any derivative works that you create.
// (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations
//     in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its
//     licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of
//     its contribution in the software or derivative works of the contribution in the software.
// 
// 3. Conditions and Limitations
// (A) Reciprocal Grants- For any file you distribute that contains code from the software (in source code or binary format), 
//     you must provide recipients the source code to that file along with a copy of this license, which license will govern that file. 
//     You may license other files that are entirely your own work and do not contain code from the software under any terms you choose.
// (B) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
// (C) If you bring a patent claim against any contributor over patents that you claim are infringed by the software,
//     your patent license from such contributor to the software ends automatically.
// (D) If you distribute any portion of the software, you must retain all copyright, patent, trademark,
//     and attribution notices that are present in the software.
// (E) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy 
//     of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do 
//     so under a license that complies with this license.
// (F) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions.
//    You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your
//    local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
#endregion

using System;

namespace DnEcuDiag.Conversion
{
    class ME21TConversion : KwpConvertor, IConvertor
    {
        #region IConvertor Members

        public float Convert(int conversionId, byte[] rawData)
        {
            float result = 0;
            UInt32 rawValue = ConvertByteArray(rawData);

            switch (conversionId)
            {
                case 0:
                    result = rawValue;
                    break;
                case 50:
                    // CHECKED
                    // A function for Odometer on ME2.1
                    // A function for odometer content on last rewriting on ME2.1
                    // A function for Air Weight on ME2.1
                    result = rawValue * 0.1f;
                    break;
                case 51:
                    // CHECKED				
                    // A function for Checksum manupulation counter on ME2.1
                    // A function for number of rewritings on ME2.1
                    // A function for Error with ambient conditions on ME2.1
                    // A function for Engine RPM on ME2.1
                    // A function for target idle RPM on ME2.1
                    result = rawValue;
                    break;
                case 52:
                    // CHECKED
                    // A function for Maximum Rpm Reached on ME2.1
                    result = rawValue * 40;
                    break;
                case 53:
                    //  A function for Engine Temperature on ME2.1
                    result = Me21NonTurboEngineTemp[rawValue];
                    break;
                case 54:
                    //  A function for Air Temperature on ME2.1
                    result = Me21NonTurboAirTemp[rawValue];
                    break;
                case 55:
                    // CHECKED
                    //  A function for Advance Angle on ME2.1
                    result = ((int)rawValue - 104) * .75f;
                    break;
                case 56:
                    // CHECKED
                    // A function for injection Time on ME2.1
                    // A function for filtered load on ME2.1
                    // A function for Injection Correction Time f(VB)
                    result = rawValue * 2.66f;
                    break;
                case 57:
                    // CHECKED
                    // A function for injection Time during starting on ME2.1
                    result = rawValue * 21.33f;
                    break;
                case 58:
                    // CHECKED
                    // A function for throttle valve angle on ME2.1
                    result = rawValue * 80.0916f / 256;
                    break;
                case 59:
                    // CHECKED
                    //  A function for Standardised Load ME2.1
                    result = rawValue * 0.75f;
                    break;
                case 60:
                    // CHECKED
                    // A function for Secondary load f(Throttle Valve) on ME2.1
                    // A function for filterd secondary load f(Throttle Valve) on ME2.1
                    result = rawValue * 42.66f;
                    break;
                case 61:
                    // CHECKED
                    // A function for altitude sensor on ME2.1
                    result = (rawValue + 251.79f) * 2.2297f;
                    break;
                case 62:
                    // CHECKED
                    // A function for airflow meter voltage on ME2.1
                    result = rawValue * 4.89f;
                    break;
                case 63:
                    // CHECKED
                    // A function for battery voltage on ME2.1
                    result = rawValue * 0.0942f;
                    break;
                case 64:
                    // CHECKED
                    // A function for low resolution vehicle speed on ME2.1
                    result = rawValue / 16f;
                    break;
                case 65:
                    // CHECKED
                    //  A function for high resolution vehicle speed on ME2.1
                    result = rawValue * 1.2f;
                    break;
                case 66:
                    // CHECKED
                    // Voltage can be calculated relative to grnd
                    //  A function for i sensor pre cat row #1 on ME2.1
                    //  A function for i sensor post cat row #1 on ME2.1
                    // RLI 59 ME2.1
                    // RLI 5A ME2.1
                    result = rawValue * 19;
                    break;
                case 67:
                    // CHECKED	
                    // A function for target idle throttle valva position on ME2.1
                    // RLI 55 ME2.1
                    // RLI 56 ME2.1
                    // RLI 57 ME2.1
                    // RLI 6E ME2.1
                    // RLI 6F ME2.1
                    result = rawValue * 80.916f / 32767;
                    break;
                case 68:
                    // CHECKED
                    // RLI 47 ME2.1
                    // RLI 48 ME2.1
                    // RLI 4A ME2.1
                    // RLI 4E ME2.1
                    // RLI 4F ME2.1
                    result = rawValue * 100f / 255;
                    break;
                case 69:
                    // CHECKED
                    // RLI 4B ME2.1
                    // RLI 4C ME2.1
                    // RLI 4D ME2.1
                    result = rawValue * 100f / 1023;
                    break;
                case 70:
                    // CHECKED
                    // RLI 50 ME2.1
                    // RLI 51 ME2.1
                    // RLI 52 ME2.1
                    // RLI 53 ME2.1
                    result = rawValue * 100f / 215;
                    break;
                case 71:
                    // CHECKED
                    // RLI 58 ME2.1
                    result = rawValue / 327.67f;
                    break;
                case 72:
                    // CHECKED
                    // RLI 5B ME2.1
                    // RLI 5C ME2.1
                    // RLI 5F ME2.1
                    // RLI 60 ME2.1
                    // RLI 61 ME2.1
                    // RLI 62 ME2.1
                    // RLI 63 ME2.1
                    // RLI 64 ME2.1
                    result = (rawValue / 65536f) + 0.50f;
                    break;
                case 73:
                    // CHECKED
                    // RLI 5D ME2.1
                    // RLI 5E ME2.1
                    if ((rawValue & 0x8000) == 0)
                    {
                        result = rawValue * 2.66f;
                    }
                    else
                    {
                        rawValue = (~rawValue) & 0xFFFF; // same as 16bit int invert.  
                        result = (rawValue + 1) * -2.66f;
                    }
                    break;
                case 74:
                    // Engine Temperature turbo
                    result = Me21TurboEngineTemp[rawValue];
                    break;
                case 75:
                    // Air Temperature turbo
                    result = Me21TurboAirTemp[rawValue];
                    break;
                case 76:
                    // Alititue sensor
                    result = (rawValue + 251.79f) * 2.2297f;
                    break;
            }

            return result;
        }

        #endregion
    }
}
